//邻接矩阵----C语言
#include <stdio.h>
#include <stdlib.h>

#define inf -1  //表示两个顶点间没有边的情况

typedef struct Graph{
    int vertices;  //顶点数目
    int ** edges;  //矩阵
}Graph;

//邻接矩阵的创建
void GraphCreate(Graph * g,int vertices)
{
    g->vertices=vertices;
    g->edges=(int **)malloc(sizeof(int *)*vertices);
    for(int i=0;i<vertices;i++)
    {
        g->edges[i]=(int *)malloc(sizeof(int)*vertices);
        for(int j=0;j<vertices;j++)
        {
            g->edges[i][j]=inf;
        }
    }
}

//邻接矩阵的销毁
void GraphDestroy(Graph* g)
{
    for(int i=0;i<g->vertices;i++)
    {
        free(g->edges[i]);
    }
    free(g->edges);
    g->edges=NULL;
}

//邻接矩阵边的添加
void GraphAddEdge(Graph* g,int u,int v,int w)
{
    g->edges[u][v]=w;
}

//邻接矩阵的打印
void GraphPrint(Graph* g)
{
    for(int i=0;i<g->vertices;i++)
    {
        for(int j=0;j<g->vertices;j++)
        {
            printf("%3d ",g->edges[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    Graph graph;
    GraphCreate(&graph,5);

    GraphAddEdge(&graph,0,1,1);
    GraphAddEdge(&graph,0,2,3);
    GraphAddEdge(&graph,1,2,2);
    GraphAddEdge(&graph,2,3,7);
    GraphAddEdge(&graph,3,4,9);
    GraphAddEdge(&graph,4,0,4);
    GraphAddEdge(&graph,4,2,5);

    printf("邻接矩阵表示的图：\n");
    GraphPrint(&graph);

    GraphDestroy(&graph);

    return 0;

}